let mongoose = require('mongoose'),
    async = require('async'),
    resourcesThumbs = mongoose.model('resources_thumbs'),
    resourcesPost = mongoose.model('resources_posts');

module.exports = (req, res) => {
    let params = req.query;

    if(!params.pid){
        res.json(resFail('资源id不能为空'))
        return false;
    }

    async.series([
        // 查询资源
        callback => {
            resourcesPost.findOne({
                pid: params.pid
            }, (err, doc) => {
                if(err){
                    res.json(resFail(err))
                }else{
                    this.article = doc;
                    callback();
                }
            });
        },
        // 查询点赞数据
        callback => {
            resourcesThumbs.findOne({
                pid: params.pid
            }, (err, doc) => {
                if(err){
                    res.json(resFail(err))
                }else{
                    let nameList = doc.thumbsNameList,
                        check = false;

                    try {
                        nameList.forEach(e => {
                            if(e.mobile == params.mobile){
                                check = true;
                                throw e;
                            }
                        })
                    } catch (error) {}

                    let d = this.article,
                        obj = {
                            pid: d.pid,
                            mobile: d.mobile,
                            userName: d.userName,
                            browseCount: d.browseCount,
                            content: d.content,
                            coverImg: d.coverImg,
                            createTime: d.createTime,
                            recommend: d.recommend,
                            resourcesType: d.resourcesType,
                            status: d.status,
                            thumbsCount: d.thumbsCount,
                            title: d.title,
                            thumbState: check
                        }

                    res.json({
                        result: 1,
                        data: obj
                    })
                }
            })
        }
    ])
    
}